package com.demo2do.fms.scheduler.scheduler;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.demo2do.fms.entity.vehicle.source.VehicleSource;
import com.demo2do.fms.entity.vehicle.tractor.Tractor;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.demo2do.fms.entity.business.Shipment;
import com.demo2do.fms.entity.vehicle.source.Vehicle;
import com.demo2do.fms.entity.vehicle.source.VehicleGpsInfo;
import com.demo2do.fms.scheduler.service.ShipmentReader;
import com.demo2do.fms.scheduler.service.ShipmentWriter;
import com.demo2do.fms.scheduler.service.VehicleLockReader;
import com.demo2do.fms.scheduler.service.VehicleLockWriter;

/**
 *
 * @author Ark
 *
 */
@Component("fmsScheduler")
public class FmsScheduler {

    private static final Logger logger = LoggerFactory.getLogger(FmsScheduler.class);
    
    @Autowired
    private ShipmentReader shipmentReader;
    
    @Autowired
    private ShipmentWriter shipmentWriter;
    
    @Autowired
    private VehicleLockReader vehicleLockReader;
    
    @Autowired
    private VehicleLockWriter vehicleLockWriter;
    
    /**
     * 同步调度指令回单状态
     */
    public void syncShipment() {
        
        logger.warn("#syncShipment() - sync shipment begin...");
        
        // 1. load id section
        Map<String, BigDecimal> idSection = shipmentReader.loadUnConfirmSection();
        long minId = idSection.get("min").longValue();
        long maxId = idSection.get("max").longValue();

        // 2. sync shipment by id part
        for (long i = minId; i <= (maxId - 1000); i += 1000) {
        	
        	List<Shipment> shipments = shipmentReader.listUnElectronicConfirm(i, i + 1000);
        	shipmentWriter.sync(shipments);
        	
        }
        
        logger.warn("#syncShipment() - sync shipment end...");
        
    }
    
    /**
     * 锁定车辆
     * 		- 锁定没有司机的车辆
     * 		- 锁定年检过期的车辆
     */
    public void lockVehicle() {
    	
    	logger.warn("#lockVehicle() - lock vehicle begin...");
    	
    	// 1. lock vehicle by no driver
    	List<Vehicle> vehicles = vehicleLockReader.listNoDriverVehicle();
    	logger.warn("#lockVehicle() - lock vehicle by no driver size=[{}]", vehicles.size());
    	vehicleLockWriter.lockVehicleNoDriver(vehicles);
    	
    	// 2. lock vehicle by no gps
//    	Date lastWeek = DateUtils.addDays(new Date(), -7);
//    	List<Long> lockVids = vehicleLockReader.listVehicleByGps(lastWeek);
//    	logger.warn("#lockVehicle() - lock vehicle by no gps size=[{}]", lockVids.size());
//    	vehicleLockWriter.lockVehicleByGps(lockVids);


        // 3. 牵引车和挂车的行驶证的字段“年检有效期至”超过当日日期时，自动锁定；
        //TODO 上线等通知
//        List<VehicleSource> sources = vehicleLockReader.listAnnualInspectionDate(new Date());
//        vehicleLockWriter.lockVehicleByVehicleSource(sources);

    	logger.warn("#lockVehicle() - lock vehicle end...");
    	
    }
    
    /**
     * 车辆解锁
     *
     */
    public void unlockVehicle() {

    	try {

            logger.warn("#FmsScheduler unlockVehicle() - unlock vehicle begin...");
            // 1. unlock vehicle by gps
//    	Date twoHoursBefore = DateUtils.addHours(new Date(), -2);
//            Date twoHoursBefore = DateUtils.addHours(new Date(), -720);
//            List<VehicleGpsInfo> vehicleGpsInfoIds = vehicleLockReader.listNeedUnlockVehicle(twoHoursBefore);
//            logger.warn("#vehicleGpsInfoIds() - unlock vehicle by gps size=[{}]", vehicleGpsInfoIds.size());
//            vehicleLockWriter.unlockVehicleByGps(vehicleGpsInfoIds, twoHoursBefore);

            //自动解锁条件更改为同时满足这3个条件（1.车辆审核通过，2.司机审核通过，3车辆绑定司机。）
            vehicleLockWriter.unlockVehicle();
            logger.warn("#FmsScheduler unlockVehicle() - unlock vehicle end...");

        }catch (Exception e){

            logger.error("#FmsScheduler unlockVehicle() - Exception [{}]",e);
        }

    }
    
    /**
     * 同步MDM车辆信息
     */
    public void syncVehicleByMdm() {
        
        logger.warn("#syncVehicleByMdm() - unlock vehicle begin...");
        
        // 2. sync mdm vehicle lock status
        vehicleLockWriter.syncVehicleByMdm();
        
        logger.warn("#syncVehicleByMdm() - unlock vehicle end...");
        
    }
    
}
